
Ext.define("AM.util.GridDoActionUtil", {
	doSave   : function(grid, treeObj) {
		if(!grid) {
			alert('参数传入不正确!');
			return;
		}
		var store = grid.getStore();
		var records = store.getUpdatedRecords();
		var data = [];
		Ext.Array.each(records, function(model) {
			data.push(Ext.JSON.encode(model.data));
		});
		//alert(data);
		
		Ext.Ajax.request({
			url    : store.getProxy().api['update'],
			params : {data : data},
			method : 'POST',
			timeout: 4000,
			success: function(response, opts) {
				//alert(response.responseText);
				Ext.Array.each(records, function(model) {
					var node = treeObj.getStore().getNodeById(model.get('id'));
					node.data['text'] = model.data['text'];
					node.commit();
					model.commit();
				});
			}
		});
	},
	doInsert : function(grid, modelObj, treeObj) {
		if(!(grid && modelObj)) {
			alert('参数传入不正确!');
			return false;
		}
		//得到数据集合
		var store = grid.getStore();
		//得到数据总数
		var storeCount = store.getCount();
		//得到编辑插件
		var edit = grid.editing;
		//得到模型
		var model = store.model;
		
		if(storeCount == 0) {//添加根节点
			var deptObj = new model(modelObj);//初始化模型
			edit.cancelEdit();//取消其他插件的编辑活动
			store.insert(0, deptObj);
			edit.startEditByPosition({
				row   :0,
				column:1
			});
			
			if(treeObj) {//同时更新左边tree
				var rootNode = treeObj.getStore().getRootNode();
				rootNode.appendChild({
					id   : modelObj['id'],
					text : modelObj['text'],
					leaf : modelObj['leaf']
				});
			}
		} else {
			var checkedRecords = grid.getSelectionModel().getSelection();
			if(checkedRecords.length == 1) {
				var parentRecord = checkedRecords[0];
				var deptObj = new model(modelObj);//初始化模型
				edit.cancelEdit();//取消其他插件的编辑活动
				store.insert(0, deptObj);
				edit.startEditByPosition({
					row   :0,
					column:1
				});
			} else {
				alert('请选择一个父节点');
			}
		}
	}
});










